// var amapFile = require('../../../utils/amap-wx.js');
// var markersData = [];

const app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    locationArr: ['', '', ''],
    position: '',
    city: '',
    district: '',

    winHeight:0,
    //下面是字母排序
    letter: ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"],
    cityListId:  '',
    allCommunity: [
      {
        'name': '海淀区',
        'data': [{ "letter": "A", "data": [{ "id": "v7", "cityName": "a海淀区社区1号" }] }, { "letter": "B", "data": [{ "id": "v10", "cityName": "b海淀区社区1号" }, { "id": "v4", "cityName": "b海淀区社区2号" }, { "id": "v1", "cityName": "b海淀区社区3号" }] }, { "letter": "C", "data": [{ "id": "v15", "cityName": "c海淀区社区1号" }] }, { "letter": "D", "data": [{ "id": "v21", "cityName": "d海淀区社区4号" }] }, { "letter": "G", "data": [{ "id": "v17", "cityName": "g海淀区社区7号" }, { "id": "v29", "cityName": "g海淀区社区8号" }] }, { "letter": "H", "data": [{ "id": "v9", "cityName": "h海淀区社区8号" }, { "id": "v3", "cityName": "h海淀区社区9号" }] }, { "letter": "L", "data": [{ "id": "v24", "cityName": "L海淀区社区1号" }, { "id": "v20", "cityName": "L海淀区社区2号" }, { "id": "v14", "cityName": "L海淀区社区3号" }] }, { "letter": "M", "data": [{ "id": "v13", "cityName": "M海淀区社区1号" }] }, { "letter": "N", "data": [{ "id": "v27", "cityName": "N海淀区社区1号" }] }, { "letter": "S", "data": [{ "id": "v18", "cityName": "S海淀区社区1号" }, { "id": "v2", "cityName": "S海淀区社区2号" }] }, { "letter": "T", "data": [{ "id": "v5", "cityName": "T海淀区社区1号" }] }, { "letter": "W", "data": [{ "id": "v12", "cityName": "W海淀区社区1号" }, { "id": "v25", "cityName": "W海淀区社区2号" }] }, { "letter": "X", "data": [{ "id": "v23", "cityName": "x海淀区社区1号" }, { "id": "v28", "cityName": "x海淀区社区2号" }, { "id": "v19", "cityName": "x海淀区社区3号" }] }, { "letter": "Z", "data": [{ "id": "v8", "cityName": "z海淀区社区1号" }] }]
      },
      {
        'name': '东城区',
        "data": [{ "letter": "A", "data": [{ "id": "v7", "cityName": "a东城区社区1号" }] }, { "letter": "B", "data": [{ "id": "v10", "cityName": "b东城区社区1号" }, { "id": "v4", "cityName": "b东城区社区2号" }, { "id": "v1", "cityName": "b东城区社区3号" }] }, { "letter": "C", "data": [{ "id": "v15", "cityName": "c东城区社区1号" }] }, { "letter": "D", "data": [{ "id": "v21", "cityName": "d东城区社区4号" }] }, { "letter": "G", "data": [{ "id": "v17", "cityName": "g东城区社区7号" }, { "id": "v29", "cityName": "g东城区社区8号" }] }, { "letter": "H", "data": [{ "id": "v9", "cityName": "h东城区社区8号" }, { "id": "v3", "cityName": "h东城区社区9号" }] }, { "letter": "L", "data": [{ "id": "v24", "cityName": "L东城区社区1号" }, { "id": "v20", "cityName": "L东城区社区2号" }, { "id": "v14", "cityName": "L东城区社区3号" }] }, { "letter": "M", "data": [{ "id": "v13", "cityName": "M东城区社区1号" }] }, { "letter": "N", "data": [{ "id": "v27", "cityName": "N东城区社区1号" }] }, { "letter": "S", "data": [{ "id": "v18", "cityName": "S东城区社区1号" }, { "id": "v2", "cityName": "S东城区社区2号" }] }, { "letter": "T", "data": [{ "id": "v5", "cityName": "T东城区社区1号" }] }, { "letter": "W", "data": [{ "id": "v12", "cityName": "W东城区社区1号" }, { "id": "v25", "cityName": "W东城区社区2号" }] }, { "letter": "X", "data": [{ "id": "v23", "cityName": "x东城区社区1号" }, { "id": "v28", "cityName": "x东城区社区2号" }, { "id": "v19", "cityName": "x东城区社区3号" }] }, { "letter": "Z", "data": [{ "id": "v8", "cityName": "z东城区社区1号" }] }],
      }
    ],
    citylist: [
      { "letter": "A", "data": [{ "id": "v7", "cityName": "" }] }, { "letter": "B", "data": [{ "id": "v10", "cityName": " " }] }, { "letter": "C", "data": [{ "id": "v15", "cityName": "" }] }, { "letter": "D", "data": [{ "id": "v21", "cityName": " " }] }, { "letter": "G", "data": [{ "id": "v17", "cityName": " " }] }, { "letter": "H", "data": [{ "id": "v9", "cityName": " " }] }, { "letter": "L", "data": [{ "id": "v24", "cityName": " " }] }, { "letter": "M", "data": [{ "id": "v13", "cityName": " " }] }, { "letter": "N", "data": [{ "id": "v27", "cityName": " " }] }, { "letter": "S", "data": [{ "id": "v18", "cityName": " " }] }, { "letter": "T", "data": [{ "id": "v5", "cityName": " " }] }, { "letter": "W", "data": [{ "id": "v12", "cityName": " " }] }, { "letter": "X", "data": [{ "id": "v23", "cityName": "" }] }, { "letter": "Z", "data": [{ "id": "v8", "cityName": " " }] }
    ],
    //下面是热门城市数据，模拟数据
    newcity: ['北京', '上海', '广州', '深圳', '成都', '杭州'],
    // citySel: '全国',
    locateCity: '',
    cityShow: false,

    markers: [],
    latitude: '',
    longitude: '',
    textData: {}
  },

  //选择地址     点击所在地区弹出选择框
  chooseAddress: function (e) {
    var _this = this;
    var cityShow = true
    _this.setData({
      cityShow
    })
    console.log(this.data.cityShow)
    _this.getAddress.showsGoodsDetail();
  },
  //组件回调
  resultEvent: function (e) {
    var _this = this
    _this.setData({
      locationArr: e.detail.nameArr
    })
    _this.data.allCommunity.forEach(function(item){
      if(item.name === e.detail.nameArr[2]){
        _this.setData({
          citylist: item.data
        })
      }
    })
  },
  //点击城市
  cityTap(e) {
    console.log(e)
    const val = e.currentTarget.dataset.val || '',
      types = e.currentTarget.dataset.types || '',
      Index = e.currentTarget.dataset.index || '',
      that = this;
    let city = this.data.citySel;
    switch (types) {
      case 'locate':
        //定位内容
        city = this.data.locateCity;
        break;
      case 'national':
        //全国
        city = '全国';
        break;
      case 'new':
        //热门城市
        city = val;
        break;
      case 'list':
        //城市列表
        city = val.cityName;
        break;
    }
    if (city) {
      wx.setStorage({
        key: 'city',
        data: city
      })
      //点击后给父组件可以通过bindcitytap事件，获取到cityname的值，这是子组件给父组件传值和触发事件的方法
      this.triggerEvent('citytap', { cityname: city });
    } else {
      console.log('还没有');
      // this.getLocate();
    }
  },
  //点击城市字母
  letterTap(e) {
    const Item = e.currentTarget.dataset.item;
    this.setData({
      cityListId: Item
    });
    console.log("..............."+this.data.cityListId);
  },
  // 返回到首页
  goBackIndex(e){
    let pages = getCurrentPages();//当前页面    （pages就是获取的当前页面的JS里面所有pages的信息）
    let prevPage = pages[pages.length - 2];//上一页面（prevPage 就是获取的上一个页面的JS里面所有pages的信息）
    prevPage.setData({
      community: e.currentTarget.dataset.back,
    }) 
    wx.navigateBack({
      delta: 1
    })
  },
  
  // loadInfo: function () {
  //   var page = this
  //   wx.getLocation({
  //     type: 'gcj02', // 默认为 wgs84 返回 gps 坐标，gcj02 返回可用于 wx.openLocation 的坐标 
  //     success: function (res) {
  //       // success 
  //       var longitude = res.longitude
  //       var latitude = res.latitude
  //        console.log(longitude + " " + latitude)
  //       page.loadCity(longitude, latitude)
  //     },
  //     fail: function () {// fail 
  //     },
  //     complete: function () {
  //     // complete 
  //     }
  //     })
  //   },
  // loadCity: function (longitude, latitude) {
  //   var page = this
  //   wx.request({
  //   url: 'http://api.map.baidu.com/reverse_geocoding/v3/?ak=OntSEtwgrKDTbgiZQDTbdU8UZVYkrIPf&output=json&coordtype=wgs84ll&location=' + latitude + ',' + longitude,
  //     //这里的ak 是去百度地图api获取的需要自己登陆获取一下 地址：https://lbsyun.baidu.com/index.php?title=wxjsapi
  //   data: {},
  //   header: {
  //     'Content-Type': 'application/json'
  //   },
  //   success: function (res) {
  //     // success 
  //     console.log(res);
  //     //  var city = res.data.result.addressComponent.city;
  //     var city = res.data.result.formatted_address
  //     console.log("城市为" + city)
  //     page.setData({ locateCity: city });
  //   },
  //   fail: function () {
  //   // fail 
  //   },
  //   complete: function () {
  //   // complete 
  //   }
  //   })
  // },

  // makertap: function(e) {
  //   var id = e.markerId;
  //   var that = this;
  //   that.showMarkerInfo(markersData,id);
  //   that.changeMarkerColor(markersData,id);
  // },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that=this;
    app.getUserLocation(that);    //传入that值可以在app.js页面直接设置内容       

    var _this = this
    //获得dialog组件
    this.getAddress = this.selectComponent("#getAddress");
    wx.getSystemInfo({
      success: function (res) {
        _this.setData({
          windowWidth: res.windowWidth, //可使用窗口宽度
          windowHeight: res.windowHeight, //可使用窗口高度
        })
      }
    })
  },
  showMarkerInfo: function(data,i){
    var that = this;
    that.setData({
      textData: {
        name: data[i].name,
        desc: data[i].address
      }
    });
  },
  changeMarkerColor: function(data,i){
    var that = this;
    var markers = [];
    for(var j = 0; j < data.length; j++){
      if(j==i){
        data[j].iconPath = "选中 marker 图标的相对路径"; //如：..­/..­/img/marker_checked.png
      }else{
        data[j].iconPath = "未选中 marker 图标的相对路径"; //如：..­/..­/img/marker.png
      }
      markers.push(data[j]);
    }
    that.setData({
      markers: markers
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    const win = wx.getSystemInfoSync();
    console.log(win);
    this.setData({
      winHeight: win.windowHeight
    })

    console.log("winHeight:" + this.data.winHeight)
  }
})